#include "ButtonSlider.h"
#include "ObjMeshManager.h"
#include "ScreenManager.h"
#include "StringUtilities.h"

#include <iostream>

ButtonSlider::ButtonSlider()
{
	m_pos = D3DXVECTOR3(0.1f, 0.1f, -20.0f);
	m_posSlider = D3DXVECTOR3(0.1f, 0.1f, -21.5f);
		
	m_convertedMousePos = D3DXVECTOR2(0.0f, 0.0f);
	m_mousePos = D3DXVECTOR2(0.0f, 0.0f);
	m_screenSize = D3DXVECTOR2(0.0f, 0.0f);

	m_screenSize.x = (float)TheScreenManager::Instance()->GetWindowWidth();
	m_screenSize.y = (float)TheScreenManager::Instance()->GetWindowHeight();
	m_aspectRat = (float)m_screenSize.x / (float)m_screenSize.y;

	m_size = D3DXVECTOR3(0.5f, 0.01f, 0.0f);
	m_sizeSlider = D3DXVECTOR3(0.25f, 0.01f, 0.0f);
	if (m_screenSize.x > m_screenSize.y)
	{
		m_size.y *= m_aspectRat;
		m_sizeSlider.y *= m_aspectRat;
	}
	else if (m_screenSize.y > m_screenSize.x)
	{
		m_size.x *= m_aspectRat;
		m_sizeSlider.x *= m_aspectRat;
	}
	m_sizeSliderOrig = m_sizeSlider;
	m_posSlider.x -= m_sizeSliderOrig.x;

	m_outlineSize = 0.01f;
	m_mouseDown = false;
	m_mouseDelta = 0;
	m_mouseDownPos = D3DXVECTOR2(0.0f, 0.0f);
	m_colSlider = D3DXVECTOR4(0.5f, 0.5f, 0.5f, 1.0f);
	m_oldMouseX = 0.0f;
	m_mouseMoved = false;
	m_mouseIntersected = false;
	m_mouseDidIntersect = false;
	m_mouseButtonWasReleased = false;
	m_amount = 0.0f;

	m_text.SetFont("pixel_small", L"../../Assets/pixel2.png");
	m_textSize = m_text.GetSize();

	m_sizeSlider.x = m_size.x;

	TheEventPoller::Instance()->AddHandler(this);
}

ButtonSlider::~ButtonSlider()
{
	TheEventPoller::Instance()->RemoveHandler(this);
}

void ButtonSlider::Draw()
{
	// background of bar
	TheObjMeshManager::Instance()->SetColour("bar", D3DXVECTOR4(0.0f, 0.0f, 0.0f, 1.0f));
	TheObjMeshManager::Instance()->Translate("bar", D3DXVECTOR3(m_pos.x, m_pos.y * 0.5f, m_pos.z + 0.5f));
	TheObjMeshManager::Instance()->Scale("bar", D3DXVECTOR3(m_size.x + m_outlineSize,
															m_size.y + m_outlineSize,
															m_size.z));
	TheObjMeshManager::Instance()->Draw("bar");

	TheObjMeshManager::Instance()->SetColour("bar", D3DXVECTOR4(1.0f, 1.0f, 1.0f, 1.0f));
	TheObjMeshManager::Instance()->Translate("bar", D3DXVECTOR3(m_pos.x, m_pos.y * 0.5f, m_pos.z));
	TheObjMeshManager::Instance()->Scale("bar", m_size);
	TheObjMeshManager::Instance()->Draw("bar");

	if (m_mouseDown)
	{
		if (m_mouseIntersected)
		{
			m_colSlider = D3DXVECTOR4(0.7f, 0.7f, 0.7f, 1.0f);
			if (m_mouseMoved)
			{
				m_sizeSlider.x = ((m_convertedMousePos.x * 0.5f) + (m_size.x * 0.5f)) - m_pos.x;

				// clamp bar
				if (m_sizeSlider.x > m_size.x)
				{
					m_sizeSlider.x = m_size.x;
				}
				else if (m_sizeSlider.x < 0.0f * m_aspectRat)
				{
					m_sizeSlider.x = 0.0f * m_aspectRat;
				}

				m_amount = m_sizeSlider.x * 2.0f;
			}
			m_mouseMoved = false;
		}
	}
	else
	{
		m_colSlider = D3DXVECTOR4(0.5f, 0.5f, 0.5f, 1.0f);
	}

	TheObjMeshManager::Instance()->SetColour("bar", m_colSlider);
	TheObjMeshManager::Instance()->Translate("bar", D3DXVECTOR3(m_sizeSlider.x * 0.5f, 0.0f, 0.0f));
	TheObjMeshManager::Instance()->Translate("bar", D3DXVECTOR3(m_posSlider.x, m_posSlider.y * 0.5f, m_posSlider.z));
	TheObjMeshManager::Instance()->Scale("bar", m_sizeSlider);
	TheObjMeshManager::Instance()->Draw("bar");
	
	m_text.SetColour(D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f));
	m_text.Translate(D3DXVECTOR3(((m_pos.x * 0.5f) + (((float)m_name.size() * 0.75f) * m_textSize)) * -1.0f, m_pos.y, 0.0f));
	m_text.Update(m_name);
	m_text.Draw();
}

void ButtonSlider::IntersectedMouse()
{
	D3DXVECTOR2 pos = D3DXVECTOR2(m_pos.x, m_pos.y);
	//pos.x += m_sizeSlider.x * 0.5f;

	float maxX = (pos.x * 2.0f) + m_size.x;
	float minX = (pos.x * 2.0f) - m_size.x;
	float maxY = pos.y + m_size.y;
	float minY = pos.y - m_size.y;
	
	if (m_convertedMousePos.x > minX && m_convertedMousePos.x < maxX)
	{
		if (m_convertedMousePos.y > minY && m_convertedMousePos.y < maxY)
		{
			m_mouseIntersected = true;
		}
	}
} 

void ButtonSlider::SetSound(Sound* s)
{
	m_sound = s;
}

//void ButtonSlider::IntersectedMouse()
//{
//	D3DXVECTOR2 pos = D3DXVECTOR2(m_posSlider.x, m_posSlider.y);
//	//pos.x += m_sizeSlider.x * 0.5f;
//
//	float maxX = (pos.x - (m_sizeSlider.x) + (0.5f)) * -2.0f;
//	float minX = pos.x - 0.25f;
//	float maxY = pos.y + (m_sizeSlider.y);
//	float minY = pos.y - (m_sizeSlider.y);
//	
//	if (m_convertedMousePos.x > minX && m_convertedMousePos.x < maxX)
//	{
//		if (m_convertedMousePos.y > minY && m_convertedMousePos.y < maxY)
//		{
//			m_mouseIntersected = true;
//		}
//	}
//} 

float ButtonSlider::GetAmount() const
{
	return m_amount;
}

void ButtonSlider::SetPos(const D3DXVECTOR2& pos)
{
	m_pos.x = pos.x;
	m_pos.y = pos.y;

	m_posSlider.x = pos.x - m_sizeSliderOrig.x;
	m_posSlider.y = pos.y;
}

void ButtonSlider::OnMouseMotionEvent(const LPARAM& event)
{
	int x = LOWORD(event);
	int y = HIWORD(event);
	m_mousePos = D3DXVECTOR2(float(x), float(y));
	m_convertedMousePos.x = (m_mousePos.x - (m_screenSize.x * 0.5f)) / (m_screenSize.x * 0.5f);
	m_convertedMousePos.y = (m_mousePos.y - (m_screenSize.y * 0.5f)) / (m_screenSize.y * 0.5f) * -1.0f;
	
	if (m_mouseDown)
	{
		m_mouseDelta = 0.0f;
		
		static float m_scrollSpeed = 0.002f;
		if (m_oldMouseX > x)
		{
			m_mouseDelta = -m_scrollSpeed;
		}
		else if (m_oldMouseX < x)
		{
			m_mouseDelta = m_scrollSpeed;
		}
		m_oldMouseX = x;
	}
	else
	{
		m_mouseDelta = 0;
	}

	m_mouseMoved = true;
}

void ButtonSlider::OnMouseButtonDownEvent(const MSG& event)
{
	if (event.message == WM_LBUTTONDOWN)
	{
		m_mouseDown = true;
		m_mouseDownPos = D3DXVECTOR2((LOWORD(event.lParam)), (HIWORD(event.lParam)));
		IntersectedMouse();
	}
}

void ButtonSlider::OnMouseButtonUpEvent(const MSG& event)
{
	if (event.message == WM_LBUTTONUP)
	{
		m_mouseDown = false;

		if (m_mouseIntersected)
		{
			m_mouseDidIntersect = true;
		}

		m_mouseIntersected = false;
		m_mouseButtonWasReleased = true;
	}
}